package com.cbgolf.oa.widget;


import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.util.DensityUtil;

public class CustomProgressBarDialog extends Dialog {

//    static Handler handler = new Handler(Looper.getMainLooper());
//    static Runnable run = new Runnable() {
//        @Override
//        public void run() {
//
////		AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
////		animationDrawable.start();
//            if (null != iv && null != a) {
//                iv.startAnimation(a);
//                handler.postDelayed(this, 800);
//            }
//        }
//    };

    private static ImageView iv;
    private static Animation a;

    public static boolean CancelAble = true;

    public CustomProgressBarDialog(Context context) {
        super(context);
    }

    public CustomProgressBarDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomProgressBarDialog createDialog(Context context) {
        CustomProgressBarDialog customProgressDialog = new CustomProgressBarDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.dialog_custom_progressbar);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        Window w = customProgressDialog.getWindow();
        WindowManager.LayoutParams wl = w.getAttributes();
//        WindowManager.LayoutParams wl = customProgressDialog.getWindow().getAttributes();
        wl.alpha = 0.5f;
        wl.dimAmount = 0f;
        customProgressDialog.getWindow().setAttributes(wl);
        onWindowFocusChanged(context, customProgressDialog, true);
        customProgressDialog.setCancelable(CancelAble);
        return customProgressDialog;
    }

    public static CustomProgressBarDialog createDialog(Context context, int gravity) {
        CustomProgressBarDialog customProgressDialog = new CustomProgressBarDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.dialog_custom_progressbar);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER_HORIZONTAL;
        Window w = customProgressDialog.getWindow();
        WindowManager.LayoutParams wl = w.getAttributes();

//        WindowManager.LayoutParams wl = customProgressDialog.getWindow().getAttributes();
        wl.alpha = 0.5f;
        wl.dimAmount = 0f;
        wl.y = (w.getWindowManager().getDefaultDisplay().getHeight() / 2) - DensityUtil.dip2px(context, 220);
        customProgressDialog.getWindow().setAttributes(wl);
        customProgressDialog.
                onWindowFocusChanged(context, customProgressDialog, true);
        customProgressDialog.setCancelable(CancelAble);
        return customProgressDialog;
    }

    private static void onWindowFocusChanged(Context context, CustomProgressBarDialog pd, boolean hasFocus) {
        if (pd == null) {
            return;
        }
        iv = (ImageView) pd.findViewById(R.id.dialog_iv);
        a = AnimationUtils.loadAnimation(context, R.anim.rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();
        a.setInterpolator(lin);
        iv.startAnimation(a);

//        handler.postDelayed(run, 0);
//		AnimationSet animationSet = new AnimationSet(true);
//
//		//参数1：从哪个旋转角度开始
//
//		//参数2：转到什么角度
//
//		//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//
//		//参数3：确定x轴坐标的类型，有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
//
//		//参数4：x轴的值，0.5f表明是以自身这个控件的一半长度为x轴
//
//		//参数5：确定y轴坐标的类型
//
//		//参数6：y轴的值，0.5f表明是以自身这个控件的一半长度为x轴
//
//		RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
//
//				Animation.RELATIVE_TO_SELF,0.5f,
//
//				Animation.RELATIVE_TO_SELF,0.5f);
//
//		rotateAnimation.setDuration(1000);
//
//		animationSet.addAnimation(rotateAnimation);
//		imageView.startAnimation(animationSet);


    }

    public CustomProgressBarDialog setMessage(Context context, CustomProgressBarDialog pd, String strMessage) {
//		TextView tvMsg = (TextView) pd.findViewById(R.id.c_tv_loadingmsg);
//		if (tvMsg != null) {
//			tvMsg.setText(strMessage);
//		}
        return pd;
    }

    // 显示滚动进度
    public static CustomProgressBarDialog showProgressDialog(Context context, String strMessage, boolean cancelAble) {
        CancelAble = cancelAble;
        CustomProgressBarDialog pd = createDialog(context);
        pd.setMessage(context, pd, strMessage);
        try {
            pd.show();
        } catch (Exception ee) {

        }
        return pd;
    }

    public static CustomProgressBarDialog showProgressDialog(Context context, boolean cancelAble, int gravity) {
        CancelAble = cancelAble;
        CustomProgressBarDialog pd = createDialog(context, gravity);
        try {
            pd.show();
        } catch (Exception e) {

        }
        return pd;
    }

}
